package com.jsoft.controller;

import com.alibaba.fastjson.JSON;
import com.jsoft.entity.Instituty;
import com.jsoft.service.InstitutyService;
import com.jsoft.service.impl.InstitutyServiceImpl;
import com.jsoft.utils.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 金融机构表的控制层
 * @author snkstart
 * @create 2023-02-21 9:00
 */
@WebServlet("*.in")
public class InstitutyController extends HttpServlet {
    private InstitutyService institutyService=new InstitutyServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1,servletPath.length() - 3);

        try {
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    protected void selectInstituty(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        List<Instituty> lists = institutyService.selectAllInstituty();
        writer.write(JSON.toJSONString(new ResultUtil("instis",lists)));
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
